package pl.umk.mat.fred.unicore.urc.storagemanagement.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.PlatformUI;

import de.fzj.unicore.wsrflite.xmlbeans.BaseFault;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.BelowStorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.CatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.Node;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.StorageBrowserView;

public class DeleteAction extends Action {

	StorageBrowserView view;
	
	public DeleteAction(StorageBrowserView view) {
		this.view = view;
		this.setText("Delete");
	}
	
	public void run() {
		IStructuredSelection selection = (IStructuredSelection)view.getTreeViewer().getSelection();
		
		final List<BelowStorageNode> nodes2Remove = new ArrayList<BelowStorageNode>();
		boolean multiplyCatalog2Remove = false;
		for (Object o : selection.toList()) {
			if (o instanceof MultiplyCatalogNode) {
				multiplyCatalog2Remove = true;
			}
			nodes2Remove.add((BelowStorageNode) o);
		}
		
		if (multiplyCatalog2Remove && 
				!MessageDialog.openConfirm(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
				"Confirm", "You want to remove catalog witch is linked with many other catalogs, are you sure you want to remove them?")) {
			return;
		} else 	if (!multiplyCatalog2Remove && !MessageDialog.openConfirm(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
				"Confirm", "Delete file" + ((selection.size() == 1)?"":"s") + "?")) {
			return;
		}
		
		Job job = new Job("Deleting files") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				List<Node> toRefresh = new ArrayList<Node>();
				monitor.beginTask("deleting", nodes2Remove.size() > 1 ? nodes2Remove.size() : IProgressMonitor.UNKNOWN);
				try {
					for (BelowStorageNode n : nodes2Remove) {
						try {
							if (monitor.isCanceled()) {
								return Status.CANCEL_STATUS;
							}
							if (n instanceof MultiplyCatalogNode) {
								MultiplyCatalogNode mc = (MultiplyCatalogNode) n;
								for (CatalogNode cn : mc.getCatalogs()) {
									Node parent = cn.getParent();
									if (!toRefresh.contains(parent)) {
										toRefresh.add(parent);
									}
								}
								removeMultiplyCatalogNode(mc);
							} else if (n instanceof BelowStorageNode) {
								Node parent = n.getParent();
								if (!toRefresh.contains(parent)) {
									toRefresh.add(parent);
								}
								removeBelowStorageNode((BelowStorageNode) n);
							}
							
						} finally {
							monitor.worked(1);
						}
					}
				} finally {
					monitor.done();
					for (Node n : toRefresh) {
						view.getDoubleClickAction().run(n);
					}
				}
				return Status.OK_STATUS;
			}

		};
		job.schedule();
	}
	
	private void removeBelowStorageNode(BelowStorageNode n) {
		try {
			n.getStorage().delete(n.getPath());
		} catch (BaseFault e) {
		}
	}

	private void removeMultiplyCatalogNode(MultiplyCatalogNode n) {
		for (CatalogNode cn : n.getCatalogs()) {
			try {
				cn.getStorage().delete(cn.getPath());
			} catch (BaseFault e) {
			}
		}
	}
}

